package me.project.jobster.service;

import me.project.jobster.dao.mybatis.mapper.FollowMapper;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Follow;
import me.project.jobster.dao.mybatis.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mbryce
 * @date 2018/5/1
 */
@Service
public class FollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     *
     * @param userId
     * @param companyId
     */
    public boolean followCompany(String userId, String companyId){
        Follow follow = new Follow();
        follow.setsId(userId);
        follow.setcId(companyId);
        int count = followMapper.selectCount(follow);
        if (count > 0){
            return false;
        }

        Follow f = new Follow();
        f.setsId(userId);
        f.setcId(companyId);
        followMapper.insert(f);
        return true;
    }

    /**
     * get follower
     * @param companyId
     * @return
     */
    public List<Student> getFollower(String companyId){
        String sql = "select a.* from student a where a.s_id in(select s_id from follow b where b.c_id ='" + companyId + "')";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Student.class));
    }

    /**
     * get follow
     * @param userId
     * @return
     */
    public List<Company> getMyFollow(String userId){
        String sql = "select a.* from company a where a.c_id in(select c_id from follow b where b.s_id ='" + userId + "')";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Company.class));
    }
}
